package com.parkwalk.xxx.component.tab;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

import com.parkwalk.xxx.component.AbstractPanelWithMouseEvent;
import com.parkwalk.xxx.util.SwingUtil;

public class Menu extends AbstractPanelWithMouseEvent {

	private static final long serialVersionUID = -761579672952065827L;
	private String text;
	private Page page;
	private PageButtonThread thread;
	private Color bgColor = new Color(224, 235, 247);

	private static ImageIcon leftImage = new ImageIcon(Menu.class.getResource("/images/left.png"));
	private static ImageIcon rightImage = new ImageIcon(Menu.class.getResource("/images/right.png"));
	private static ImageIcon leftImage2 = new ImageIcon(Menu.class.getResource("/images/left2.png"));
	private static ImageIcon rightImage2 = new ImageIcon(Menu.class.getResource("/images/right2.png"));

	public Menu(String text, Page page) {
		super();
		this.page = page;
		this.setPreferredSize(new Dimension(text.length() * new Font("微软雅黑", Font.PLAIN, 14).getSize() + 20, 29));
		this.text = text;
		setBackground(bgColor);

	}

	public void mouseClicked(MouseEvent e) {
		page.showPage();
	}

	public void unSelected() {
		thread = new PageButtonThread(this, -1);
		thread.start();
	}

	public void selected() {
		thread = new PageButtonThread(this, 1);
		thread.start();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2D = (Graphics2D) g;
		if (page.isSelect()) {
			g.drawImage(leftImage.getImage(), 0, 0, this);
			g.drawImage(rightImage.getImage(), this.getWidth() - 5, 0, this);
			g.setColor(new Color(186, 201, 219));
			g.drawLine(3, 0, this.getWidth() - 5, 0);
		} else {
			g.setColor(new Color(186, 201, 219));
			g.drawLine(0, this.getHeight() - 1, this.getWidth(), this.getHeight() - 1);
		}

		System.out.println("isMouseEntered: " + isMouseEntered);
		if (isMouseEntered) {
			g.drawImage(leftImage2.getImage(), 1, 0, this);
			g.drawImage(rightImage2.getImage(), this.getWidth() - 4, 0, this);
			g.setColor(new Color(250, 184, 15));
			g.drawLine(3, 0, this.getWidth() - 5, 0);
			g.setColor(this.getBackground());
			g.drawLine(3, 2, 3, this.getHeight() - 2);
			g.drawLine(4, 1, 4, this.getHeight() - 2);
			g.drawLine(getWidth() - 4, 2, getWidth() - 4, this.getHeight() - 2);
			g.drawLine(getWidth() - 5, 1, getWidth() - 5, this.getHeight() - 2);
			g.drawLine(getWidth() - 3, 3, getWidth() - 3, this.getHeight() - 2);
		}

		SwingUtil.setFractionalmetricsOn(g2D);

		g2D.setColor(new Color(21, 66, 139));
		g2D.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		g2D.drawString(text, 10, 20);
	}

	class PageButtonThread extends Thread {
		private Menu pb;
		private int aspect = 1;//
		private Color[] colors = { new Color(255, 255, 255), new Color(245, 250, 253), new Color(238, 245, 251),
				new Color(231, 240, 249), new Color(224, 235, 247), };//

		public PageButtonThread(Menu pb, int aspect) {
			this.pb = pb;
			this.aspect = aspect;
		}

		public void run() {
			if (aspect == -1) {
				for (Color c : colors) {
					pb.setBackground(c);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			} else {
				for (int i = colors.length - 1; i >= 0; i--) {
					pb.setBackground(colors[i]);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
